package com.yinp.proapp.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.hjq.permissions.XXPermissions;
import com.yinp.proapp.R;

import java.util.List;
import java.util.Objects;

/**
 * 权限弹框
 */
public class PermissionDialog extends Dialog {
    private Activity context;
    private String title;
    private List<String> permissions;

    public PermissionDialog(@NonNull Activity activity, String title, List<String> permissions) {
        super(activity, R.style.NormalDialogStyle);
        this.context = activity;
        this.title = title;
        this.permissions = permissions;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    private void initView() {
        @SuppressLint("InflateParams") View view = LayoutInflater.from(context).inflate(R.layout.dialog_permission, null);
        TextView tvLeft = view.findViewById(R.id.dialog_left);
        TextView tvRight = view.findViewById(R.id.dialog_right);
        TextView tvTitle = view.findViewById(R.id.dialog_content);
        setContentView(view);
        this.setCancelable(false);
        this.setCanceledOnTouchOutside(false);
        tvTitle.setText(title);
        tvLeft.setOnClickListener(v -> dismiss());
        tvRight.setOnClickListener(v -> {
            XXPermissions.startPermissionActivity(context, permissions);
            dismiss();
        });
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        WindowManager.LayoutParams params = Objects.requireNonNull(getWindow()).getAttributes();
        params.width = displayMetrics.widthPixels * 8 / 10;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes(params);
    }
}
